//
//  CHGradientCircleView.m
//  CH-AllDemo-OC
//
//  Created by 付亚杰 on 16/7/25.
//  Copyright © 2016年 付亚杰. All rights reserved.
//

#import "CHGradientCircleView.h"
#import "UIView+Extension.h"

#define degreesToRadians(x) (M_PI*(x)/180.0) //把角度转换成PI的方式
//#define  PROGREESS_WIDTH 220 //圆直径
//#define PROGRESS_LINE_WIDTH 20 //弧线的宽度
#define PROCESS_COLOR [UIColor redColor]
#define MAIN_BLUE [UIColor blueColor]
#define MAIN_SCREEN_ANIMATION_TIME 13.0f

// RGB颜色转换（16进制->10进制）
#define UIColorFromRGB(rgbValue)\
\
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 \
alpha:1.0]

@interface CHGradientCircleView(){
    
    CAShapeLayer *_trackLayer;
    CAShapeLayer *_progressLayer;
    UIColor *_strokeColor;
    NSInteger _percent;
    
    CGFloat PROGRESS_LINE_WIDTH;
    CGFloat PROGREESS_WIDTH;
}

@end

@implementation CHGradientCircleView


- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
//        PROGREESS_WIDTH = frame.size.width;
//        PROGRESS_LINE_WIDTH = PROGREESS_WIDTH * 0.090909;
//        _percent = 0;
//        _strokeColor = UIColorFromRGB(0x97a6f9);
        self.backgroundColor = [UIColor clearColor];
        
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(0, 0, frame.size.height, frame.size.height))];
        imageView.image = [UIImage imageNamed:@"外环"];
        [self addSubview:imageView];
        
    }
    return self;
}

-(void)setPercent{

        [CATransaction begin];
        [CATransaction setDisableActions:NO];
        [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
        [CATransaction setAnimationDuration:MAIN_SCREEN_ANIMATION_TIME];
        _progressLayer.strokeEnd = 70/100.0;
        [CATransaction commit];

    
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    
    _trackLayer = [CAShapeLayer layer];//创建一个track shape layer
    _trackLayer.frame = self.bounds;
    _trackLayer.fillColor = [[UIColor clearColor] CGColor];
    _trackLayer.strokeColor = [_strokeColor CGColor];//指定path的渲染颜色
//    _trackLayer.opacity = 0.25; //背景同学你就甘心做背景吧，不要太明显了，透明度小一点
    _trackLayer.lineCap = kCALineCapRound;//指定线的边缘是圆的
    _trackLayer.lineWidth = PROGRESS_LINE_WIDTH;//线的宽度
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.origin.x + self.bounds.size.width * 0.5,self.bounds.origin.y + self.bounds.size.height * 0.5) radius:(PROGREESS_WIDTH-PROGRESS_LINE_WIDTH)/2 startAngle:degreesToRadians(0) endAngle:degreesToRadians(360) clockwise:YES];//上面说明过了用来构建圆形
    
    _trackLayer.path =[path CGPath]; //把path传递給layer，然后layer会处理相应的渲染，整个逻辑和CoreGraph是一致的。
    
    [self.layer addSublayer:_trackLayer];
    
    _progressLayer = [CAShapeLayer layer];
    _progressLayer.frame = self.bounds;
    _progressLayer.fillColor =  [[UIColor clearColor] CGColor];
    _progressLayer.strokeColor  = [_strokeColor CGColor];
    _progressLayer.lineCap = kCALineCapRound;
    _progressLayer.lineWidth = PROGRESS_LINE_WIDTH;
    _progressLayer.path = [path CGPath];
    _progressLayer.strokeEnd = 0;
    
    CALayer *gradientLayer = [CALayer layer];
    CAGradientLayer *gradientLayer1 =  [CAGradientLayer layer];
    gradientLayer1.frame = CGRectMake(0, 0, self.width * 0.5, self.height);
    
    [gradientLayer1 setColors:[NSArray arrayWithObjects:(id)[UIColorFromRGB(0x97a6f9) CGColor],(id)[UIColorFromRGB(0x2f36f6)CGColor], nil]];
    
    [gradientLayer1 setLocations:@[@0.5,@0.9,@1 ]];
    [gradientLayer1 setStartPoint:CGPointMake(0.5, 1)];
    [gradientLayer1 setEndPoint:CGPointMake(0.5, 0)];
    [gradientLayer addSublayer:gradientLayer1];
    

    
//    CAGradientLayer *gradientLayer2 =  [CAGradientLayer layer];
//    [gradientLayer2 setLocations:@[@0.1,@0.5,@1]];
//    gradientLayer2.frame = CGRectMake(self.width * 0.5, 0, self.width * 0.5, self.height);
//    [gradientLayer2 setColors:[NSArray arrayWithObjects:(id)[UIColorFromRGB(0x2f36f6) CGColor],(id)[UIColorFromRGB(0x97a6f9) CGColor], nil]];
//    [gradientLayer1 setStartPoint:CGPointMake(0.5, 1)];
//    [gradientLayer1 setEndPoint:CGPointMake(0.5, 0)];
//    [gradientLayer addSublayer:gradientLayer2];
//
    [gradientLayer setMask:_progressLayer]; //用progressLayer来截取渐变层
    [self.layer addSublayer:gradientLayer];
    
    [self setPercent];
    
      // Drawing code
}
*/

@end
